<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:fo="http://www.w3.org/1999/XSL/Format"
	xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
			 xmlns:s="http://www.w3.org/2001/XMLSchema"
			 xmlns:s0="http://tempuri.org/" 
			 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
			 xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
			 xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/">
  <xsl:variable name="service.name" select="//*[local-name()='service']/@name"/>
	<xsl:variable name="service.endpoint" select="//*[local-name()='service']/*[local-name()='port']/*[local-name()='address']/@location"/>
	<xsl:variable name="service.namespace" select="//*[local-name()='schema']/@targetNamespace"/>

	<xsl:output method="text"/>
	
<xsl:template match="/"> 	
&lt;%
const ENDPOINT = "<xsl:value-of select="$service.endpoint"/>" 
const NAMESPACE = "<xsl:value-of select="$service.namespace"/>" 

class <xsl:value-of select="$service.name"/>  
  
<xsl:apply-templates select="//*[local-name()='portType']"/>
	
    function envelope (data) 
        dim output
        output="" 
        output=output&amp;"&lt;soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""&gt;"                            
        output=output&amp;"&lt;soap:Body&gt;" &amp; data  
        output=output&amp;"&lt;/soap:Body&gt;"  
        output=output&amp;"&lt;/soap:Envelope&gt;"  
        envelope = output 
    end function ' invoke    

    function deserialize (data, nodename, proxy) ' invoke   
        dim mdom,result, i, node, nodes, tmp
        set mdom=CreateObject("Microsoft.XMLDOM") 
        redim result(0)
        If mdom.loadXML (data) then 
            set nodes = mdom.selectNodes ("//" &amp; nodename) 
            For Each node In nodes  
                Redim Preserve result(i)
                If Len(proxy) &gt; 0 Then
                    set tmp=eval ("new " &amp; proxy)
                    tmp.deserialize(node)
                    set result(i)=tmp
                Else
                    result(i)=node.text
                End If 	
            Next
            i = i + 1
        Else 
            MsgBox ("The response was not well-formed XML.") 
        End If
        deserialize = result
    end function ' invoke    

    function invoke (data, nodename, action, array, proxy) ' invoke   
        dim mdom,mhttp,response,result 
        i=0        
        set mhttp=CreateObject("Msxml2.XMLHTTP")                                                                                                                                                                                                                                                                     
        call mhttp.open("POST", ENDPOINT, false) 
        call mhttp.setRequestHeader("SOAPAction",action) 
        call mhttp.setRequestHeader("Content-Type","text/xml")
        mhttp.send( Me.envelope(data) )                                                                                                                                                                                                                                                                       
        result=deserialize(mhttp.responseText, nodename, proxy)  
        if Len(proxy) &gt; 0 then
            if array then
                set invoke=result
            else
                set invoke=result(0)
            end if 
        else
            if array then
                invoke=result
            else
                invoke=result(0)
            end if 
        end if 
    end function ' invoke    
end class
	
<xsl:apply-templates select="//*[local-name()='schema']/*[local-name()='complexType']"/>
%&gt;  
</xsl:template>


	<xsl:template match="//*[local-name()='element']">
	<xsl:for-each select="./*[local-name()='complexType']/*[local-name()='sequence']/*[local-name()='element']">
		<xsl:value-of select="@name"/> <xsl:if test="following-sibling::*[local-name()='element']">, </xsl:if>
	</xsl:for-each>
</xsl:template>


	<xsl:template match="//*[local-name()='operation']">
		<xsl:variable name="name" select="@name"/>
    <xsl:variable name="soapaction" select="//*[local-name()='operation'][@name=$name]/*[local-name()='operation'][@soapAction]/@soapAction"/>
		<xsl:variable name="response" select="//*[local-name()='element'][@name=concat($name,'Response')]"/>
		<xsl:variable name="type" select="$response/*[local-name()='complexType']/*[local-name()='sequence']/*[local-name()='element']"/>
		<xsl:variable name="type.name">
			<xsl:choose>
				<xsl:when test="contains($type/@type,':')">
					<xsl:value-of  select="substring-after($type/@type,':')"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$type/@type"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="type.max">
			<xsl:choose>
				<xsl:when test="$type/@maxOccurs='unbounded'">true</xsl:when>
				<xsl:otherwise>false</xsl:otherwise>
			</xsl:choose>
		</xsl:variable> 
    <xsl:variable name="type.proxy">
        <xsl:if test="substring-before($type/@type,':')!='s'"><xsl:value-of select="$type.name"/></xsl:if>
    </xsl:variable>
    <xsl:variable name="type.prefix">
        <xsl:if test="substring-before($type/@type,':')!='s'">Set </xsl:if>
    </xsl:variable>
<xsl:if test="not (preceding::*[local-name()='operation'][@name=$name])">
    Function <xsl:value-of select="@name"/>(<xsl:apply-templates select="//*[local-name()='element'][@name=$name]"/>)
        dim request
        request="&lt;<xsl:value-of select="@name"/> xmlns=""" + NAMESPACE + """&gt;"<xsl:call-template name="form.invoke">
    <xsl:with-param name="method" select="@name"/>
  </xsl:call-template>
        request=request&amp;"&lt;/<xsl:value-of select="@name"/>&gt;" 
<xsl:value-of select="concat('        ',$type.prefix,@name)"/> = Me.invoke (request, "<xsl:value-of select="@name"/>Result", "<xsl:value-of select="$soapaction"/>", <xsl:value-of select="$type.max"/>, "<xsl:value-of select="$type.proxy"/>") 
    End Function

</xsl:if>
		
	</xsl:template>

	
<xsl:template name="form.invoke">
	<xsl:param name="method"/>
	<xsl:param name="for" select="//*[local-name()='element'][@name=$method]/*[local-name()='complexType']/*[local-name()='sequence']/*[local-name()='element']"/>
	<xsl:param name="me" select="$for[1]"/>
	<xsl:param name="next" select="$for[position()!=1]"/><xsl:if test="$me"> 
        request=request&amp;"&lt;<xsl:value-of select="$me/@name"/>&gt;" &amp; <xsl:value-of select="$me/@name"/> &amp; "&lt;/<xsl:value-of select="$me/@name"/>&gt;"<xsl:if test="$next">
<xsl:call-template name="form.invoke">
<xsl:with-param name="for" select="$next"/>
</xsl:call-template>
	</xsl:if></xsl:if>
</xsl:template>

	<xsl:template match="//*[local-name()='portType']">

		<xsl:apply-templates select="./*[local-name()='operation']">
		</xsl:apply-templates>
	</xsl:template>



  <xsl:template match="//*[local-name()='schema']/*[local-name()='complexType']"> 
Class <xsl:value-of select="@name"/>
<xsl:for-each select="./*/*[local-name()='element']">
    Dim <xsl:value-of select="@name"/></xsl:for-each>
    Function deserialize (element) 
        Dim i
<xsl:for-each select="./*/*[local-name()='element']"><xsl:choose>
  <xsl:when test="@maxOccurs='unbounded'">
        Set o<xsl:value-of select="@name"/>s=element.selectNodes("<xsl:value-of select="@name"/>")  
        i = 0
        Redim <xsl:value-of select="@name"/>(i) 
        For Each <xsl:value-of select="@name"/>item in o<xsl:value-of select="@name"/>s 
            Redim Preserve <xsl:value-of select="@name"/>(i)<xsl:choose> 
          <xsl:when test="substring-before(@type,':')='s'"> 
            <xsl:value-of select="@name"/>(i) = <xsl:value-of select="@name"/>item.text</xsl:when> 
          <xsl:otherwise>
            Set o<xsl:value-of select="@name"/>=new <xsl:value-of select="substring-after(@type,':')"/> 
            o<xsl:value-of select="@name"/>.deserialize (<xsl:value-of select="@name"/>item) 
            Set <xsl:value-of select="@name"/>(i) =o<xsl:value-of select="@name"/></xsl:otherwise> 
        </xsl:choose> 
            i = i + 1
        Next  </xsl:when> 
  <xsl:otherwise><xsl:choose><xsl:when test="substring-before(@type,':')='s'">
        If not element.selectSingleNode("<xsl:value-of select="@name"/>") is Nothing Then    
<xsl:value-of select="concat('            ',@name)"/>=element.selectSingleNode("<xsl:value-of select="@name"/>").text 
        End If</xsl:when>
 
          <xsl:otherwise>
        Set o<xsl:value-of select="@name"/>=new <xsl:value-of select="substring-after(@type,':')"/> 
        o<xsl:value-of select="@name"/>.deserialize (element) 
        Set <xsl:value-of select="@name"/>=o<xsl:value-of select="@name"/></xsl:otherwise> 
        </xsl:choose></xsl:otherwise></xsl:choose></xsl:for-each> 
    End Function
End Class

</xsl:template>

</xsl:stylesheet> 
